package com.infoland.afanmall.model.address.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.Error;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.address.adapter.AddressAllAdapter;
import com.infoland.afanmall.model.address.entity.AddressAllEntity;
import com.infoland.afanmall.model.eventBus.address.EventUpdateAddress;
import com.lzy.okgo.OkGo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 展示地址Activity
 */
public class AddressAllActivity extends AppCompatActivity {
    @BindView(R.id.address_recycler)
    RecyclerView addressRecycler;
    @BindView(R.id.cart_back)
    ImageView cartBack;
    @BindView(R.id.title_layout)
    LinearLayout orderToolBar;
    @BindView(R.id.insert)
    Button addressBtInsert;
    AddressAllAdapter addressAllAdapter;

    public int getLayout() {
        return R.layout.address_show_activity;
    }

    private void initData() {
        getAllAdress();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, orderToolBar);
        if (UserKit.isLogin()) {
            initData();
        } else {
//            ToastUtils.showShort("当前登录失效，请重新登录");
//            finish();
        }

    }

    private void getAllAdress() {
        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("response200 ---->   ", "  " + response200);
                    List<AddressAllEntity> adressAllEntities = JSONArray.parseArray(response200, AddressAllEntity.class);
                    setAdapter(adressAllEntities);
                    LogKit.E("所有地址--->  ", "  " + response200);
                },
                success401 -> {
                    LogKit.E("success401 ---->   ", "  " + success401);
                },
                error -> {

                }
        ).post(CONST.GET_ALL_ADDRESS + UserKit.getAccessToken(), new JSONObject(), this);

    }

    //    /*** 根据返回数据，设置适配器**/
    private void setAdapter(List<AddressAllEntity> adressAllEntities) {
        addressRecycler.setLayoutManager(new LinearLayoutManager(this));
        addressAllAdapter = new AddressAllAdapter(adressAllEntities);
        addressRecycler.setAdapter(addressAllAdapter);
        addressAllAdapter.setAddressClickLinster((int id, int type) -> {
            if (type == 0) {
                updateAddress(id);
            } else {
                setDefaultAddress(id);
            }

//            Bundle bundle = new Bundle();
//            bundle.putInt("addressId", id);
//            ToolsKit.returnResultYouXuan(this, bundle, OrderDownActivity.activityCode);
//            finish();
        });
    }

    //设置默认地址
    private void setDefaultAddress(int id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        new OkHttpKit().send(
                response200 -> {
                    JSONObject jsonObject1 = JSONObject.parseObject(response200);
                    if (jsonObject1.getBoolean("success")) {
                        getAllAdress();
                    } else {
                        ToastUtils.showShort("操作失败");
                    }
                },
                success401 -> {
                    ToastUtils.showShort("操作失败");
                    LogKit.E("  设置默认地址---> ", success401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.UPDATE_DEFAULT_ADDRESS + UserKit.getAccessToken(), jsonObject, this);
    }

    //修改地址
    private void updateAddress(int addressId) {
        EventUpdateAddress addressEventEntity = new EventUpdateAddress(addressId);
        addressEventEntity.setIsok(true);
        EventBus.getDefault().postSticky(addressEventEntity);
        ToolsKit.go(AddressInsertActivity.class);
    }

    @OnClick({R.id.cart_back, R.id.insert})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            /*** 退出**/
            case R.id.cart_back:
                finish();
                break;
            /*** 添加地址**/
            case R.id.insert:
                EventUpdateAddress addressEventEntity = new EventUpdateAddress();
                addressEventEntity.setIsok(false);
                EventBus.getDefault().postSticky(addressEventEntity);
                ToolsKit.go(AddressInsertActivity.class);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getAllAdress();
    }

    @Override
    protected void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        super.onDestroy();
    }
}
